package com.gxa.controller;

import com.gxa.common.uitls.CodeServlet;
import com.gxa.common.uitls.GyUitls;
import com.gxa.common.uitls.JwtUtils;
import com.gxa.common.uitls.R;
import com.gxa.dto.LoginDto;
import com.gxa.dto.UserDto;
import com.gxa.entity.User;
import com.gxa.oauth.OAuth2Token;
import com.gxa.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "登陆判断接口")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private CodeServletController codeServletController;

    @PostMapping("/login")
    @ApiOperation("登录请求")
    public R login(@RequestBody LoginDto loginDto){

        Subject subject = SecurityUtils.getSubject();
        //SimpleHash simpleHash=new SimpleHash("MD5",password,"ukc8BDbRigUDaY6pZFfWus2jZWLPHOukc8BDbRigUDaY6pZFfWus2jZWLPHO",1024);
        //System.out.println("-----------------password+salt:---------"+simpleHash.toString());

        //验证码
        String code = this.codeServletController.code();
        System.out.println("返回的正确的验证码是-----------"+code);

        if (!loginDto.getVerification().equals(code))
        {
            return R.error("验证码输入错误！");
        }
        else
        {
            //String jwtToken;

            //if (!JwtUtils.checkToken(request))
            //{
                //根据传过来的用户名 和 密码创建一个token
            String jwtToken = JwtUtils.getJwtToken(loginDto.getUsername(),loginDto.getPassword());
            //}
            //转换成OAuth2Token
            OAuth2Token oAuth2Token = new OAuth2Token(jwtToken);

            String passwordSalt = GyUitls.salt(loginDto.getPassword()).toString();

            User user = this.userService.queryByOldPwdAndUname(loginDto.getUsername(), passwordSalt);

            if (user == null)
            {
                return R.error("用户名或者密码错误！");
            }

            try {
                //subject.login(oAuth2Token);

                Map<String,Object> map = new HashMap<>();
                map.put("token",jwtToken);
                map.put("username",loginDto.getUsername());
                map.put("power",user.getLimit());
                System.out.println("map------------"+map.toString());
                return R.ok(map);
            } catch (Exception e) {
                e.printStackTrace();
                return R.error("登录失败");
            }
        }
    }

    @PostMapping("/user/loginout")
    @ApiOperation("退出登陆")
    public R loginout(@RequestBody UserDto userDto){
        try {
            this.userService.loginOut(userDto);
            return  R.ok("退出登录成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("退出登录失败");
        }

    }
}
